//
//  DHCommentListViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/9.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCommentListViewController.h"
#import "DHArticleCommentCell.h"
#import "DHArticleNO_CommentCell.h"
#import "FashionArticle.h"
#import "DHAddCommentViewController.h"

@interface DHCommentListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSString *articleID;
@property (strong, nonatomic) NSMutableArray *commentsArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation DHCommentListViewController

- (instancetype)initWithArticleID:(NSString *)articleID
{
    self = [super init];
    if (self) {
        self.articleID = articleID;
    }
    return self;
}

- (NSMutableArray *)commentsArray{
    if (!_commentsArray) {
        _commentsArray = [NSMutableArray array];
    }
    return _commentsArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"全部评论";
    
    [self setupUI];
    [self loadCommentsData];
}

- (void)setupUI{
    [self.tableView registerNib:[UINib nibWithNibName:@"DHArticleCommentCell" bundle:nil] forCellReuseIdentifier:kDHArticleCommentCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHArticleNO_CommentCell" bundle:nil] forCellReuseIdentifier:kDHArticleNO_CommentCell];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navView.width - 74, self.navView.height - 44, 64, 44)];
    [doneButton setTitle:@"发表评论" forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [doneButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
    [self.navView addSubview:doneButton];
    WeakSelf(weakSelf)
    [doneButton bk_addEventHandler:^(id sender) {
        DHAddCommentViewController *acController = [[DHAddCommentViewController alloc] initWithArticleID:weakSelf.articleID];
        [weakSelf.navigationController pushViewController:acController animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadCommentsData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreCommentsData)];
}

- (void)loadCommentsData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_articles_commentWithArticleID:self.articleID pageIndex:@(self.pageIndex) pageSize:@(20) andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.commentsArray removeAllObjects];
        [self.commentsArray addObjectsFromArray:[data objectForKey:@"comments"]];
        [self.tableView reloadData];
        if (self.commentsArray.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        if (self.commentsArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = NO;
        }
        self.pageIndex += 1;
    }];
}

- (void)loadMoreCommentsData{
    [[DHNetAPIClient shareDHNetAPIClient] request_articles_commentWithArticleID:self.articleID pageIndex:@(self.pageIndex) pageSize:@(20) andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.commentsArray = [data objectForKey:@"comments"];
        [self.tableView reloadData];
        if ([[data objectForKey:@"comments"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.commentsArray.count > 0) {
        return self.commentsArray.count;
    }else{
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.commentsArray.count > 0) {
        return 146;
    }else{
        return 40;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.commentsArray.count > 0) {
        DHArticleCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHArticleCommentCell forIndexPath:indexPath];
        [cell updateWithDic:[self.commentsArray objectAtIndex:indexPath.row]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else{
        DHArticleNO_CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHArticleNO_CommentCell forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
}

@end
